<?php
/* hjl:后台-课程管理模块 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once('../includes/cls_image.php');
$img = new cls_image();
$exc = new exchange($ecs->table("course"), $db, 'course_id', 'title');
/* act操作项的初始化 */
$_REQUEST['act'] = trim($_REQUEST['act']);
if (empty($_REQUEST['act']))
{
    $_REQUEST['act'] = 'list';
}

/*------------------------------------------------------ */
//-- 课程列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    $courses = course_list();
    $smarty->assign('course_list',$courses);

    $smarty->assign('ur_here',     '课程');
    $smarty->assign('action_link', array('text' => '添加课程', 'href' => 'course.php?act=add'));

    $smarty->assign('full_page',   1);
    $smarty->display('course_list.htm');


}

/*------------------------------------------------------ */
//-- 查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $courses = course_list();
   
    $smarty->assign('course_list',$courses);
    $smarty->assign('ur_here',     $_LANG['02_articlecat_list']);
    $smarty->assign('action_link', array('text' => $_LANG['articlecat_add'], 'href' => 'course.php?act=add'));
    make_json_result($smarty->fetch('course_list.htm'));
}


/*------------------------------------------------------ */
//-- 添加课程
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
    admin_priv('article_cat');
    $smarty->assign('ur_here',    '添加课程');
    $smarty->assign('action_link', array('text' => '课程列表', 'href' => 'course.php?act=list'));
    $course_list = course_list();
    foreach($course_list as $k=>$v){
         $course_list[$k]['title']=str_repeat('&nbsp;', $v['level']*4).$v['title'];
    }
    //print_r($course_list);exit;
    $smarty->assign('course_list',$course_list);
    $smarty->assign('form_action', 'insert');

    $smarty->display('course_info.htm');

    
}

/*入库*/
elseif ($_REQUEST['act'] == 'insert')
{
    /* 权限判断 */
    admin_priv('article_cat');
    //接收数据
    $title=trim($_POST['title']);
    $description=$_POST['description'];
    $price = $_POST['price'];
    $valid_day =$_POST['valid_day'];
    $time=gmtime(); //当前的时间戳
    $end_time = $time+($_POST['valid_day']*86400);
    $parent_id=$_POST['parent_id'];
    $sort_order=$_POST['sort_order'];
   /*检查课程名是否重复*/
    $is_only = $exc->is_only('title', $_POST['title']);

    if (!$is_only)
    {
        sys_msg(sprintf($_LANG['catname_exist'], stripslashes($_POST['cat_name'])), 1);
    }
   //接受上传的封面图
 
    $thumb_img =$img->upload_image($_FILES['thumb_img']);
    $add_time = time();

   
    $sql = "INSERT INTO ".$ecs->table('course')."(title, description, thumb_img,add_time, price,valid_day,parent_id,sort_order)
           VALUES ('$title', '$description',  '$thumb_img','$add_time', '$price','$valid_day','$parent_id','$sort_order')";
    $db->query($sql);
   
    $link[0]['text'] = $_LANG['continue_add'];
    $link[0]['href'] = 'course.php?act=add';

    $link[1]['text'] = $_LANG['back_list'];
    $link[1]['href'] = 'course.php?act=list';
    clear_cache_files();
   sys_msg($_POST['cat_name'].$_LANG['catadd_succed'],0, $link);
}

/*------------------------------------------------------ */
//-- 编辑课程
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    admin_priv('article_cat');

    $sql = "SELECT * FROM ".
           $ecs->table('course'). " WHERE course_id='$_REQUEST[course_id]'";
    $course_info = $db->GetRow($sql);
    //print_r($course_info);exit;
    assign_query_info();
    $smarty->assign('course_info', $course_info);
    $smarty->assign('form_action', 'update');

    $smarty->display('course_info.htm');
}

elseif ($_REQUEST['act'] == 'update')
{   //print_r($_FILES);exit;
    /* 权限判断 */
    admin_priv('article_cat');
     //print_r($_POST);exit;
     $time = gmtime();
     $end_time = ($_POST['valid_day']*86400)+$time;
    /*检查重名*/
    if ($_POST['title'] != $_POST['old_title'])
    {
        $is_only = $exc->is_only('title', $_POST['title'], $_POST['course_id']);

        if (!$is_only)
        {
            sys_msg(sprintf($_LANG['catname_exist'], stripslashes($_POST['title'])), 1);
        }
    }

    $sql = "select * from {$GLOBALS['ecs']->table('course')} where course_id='$_POST[course_id]'";
    $course_info = $GLOBALS['db']->getRow($sql);

    //接受上传的封面图
    if($_FILES['thumb_img']['name'] && $_FILES['thumb_img']['error']==0){

        $thumb_img =$img->upload_image($_FILES['thumb_img']);
    }else{

    	$thumb_img = $course_info['thumb_img'];
    }
   

      if ($exc->edit("title = '$_POST[title]', description ='$_POST[description]', price='$_POST[price]',thumb_img='$thumb_img',valid_day='$_POST[valid_day]',end_time='$end_time',sort_order='$_POST[sort_order]'",  $_POST['course_id']))
    {

        $link[0]['text'] = $_LANG['back_list'];
        $link[0]['href'] = 'course.php?act=list';
        $note = sprintf($_LANG['catedit_succed'], $_POST['cat_name']);
        clear_cache_files();
        sys_msg($note, 0, $link);

    }
    else
    {
        die($db->error());
    }
   
          
    
}

/*------------------------------------------------------ */
//-- 删除课程分类
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{   
    check_authz_json('article_cat');
   
    $id = intval($_GET['id']);
   
  
    // $sql = "SELECT COUNT(*) FROM " . $ecs->table('article_cat') . " WHERE parent_id = '$id'";
    // if ($db->getOne($sql) > 0)
    // {
    //     /* 还有子分类，不能删除 */
    //     make_json_error($_LANG['is_fullcat']);
    // }

    /* 非空的课程不允许删除 */
    $sql = "SELECT COUNT(*) FROM ".$ecs->table('course_vedio')." WHERE course_id = '$id'";
   
    if ($db->getOne($sql) > 0)
    {
        make_json_error(sprintf($_LANG['not_emptycat']));
    }
    else
    {   
        $exc->drop($id);
        
        clear_cache_files();
        
    }
    
    $url = 'course.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
   
    ecs_header("Location: $url\n");
    exit;
}


//取出所有课程
function course_list(){

   $sql="select * from {$GLOBALS['ecs']->table('course')}";
   $course_list=$GLOBALS['db']->getAll($sql);
   //排序
    $course_list=sort_cat($course_list,0);
     //print_r($course_list);exit;

   foreach($course_list as $k=>$v){
   	 $course_list[$k]['add_time'] = local_date('Y-m-d H:i:s',$v['add_time']);
   	 $course_list[$k]['thumb_img']='../'.$v['thumb_img'];
   
   	 $course_list[$k]['description']=sub_str($v['description'],100,true);
   }

   return  $course_list;

}

//排序函数
function sort_cat($cat,$parent_id,$level=0){
   static $arr=array();
   foreach($cat as $k=>$v){
     if($v['parent_id']==$parent_id){
        $v['level']=$level;
        $arr[]=$v;
        unset($cat[$k]);
        sort_cat($cat,$v['course_id'],$level+1);
     }
   }
  return $arr;
}
